package com.mujin.keji.collectionsystem.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.utils.DisplayUtil;

/**
 * 选择拜访案件条件对话框
 *
 * @author weishixiong
 * @Time 2018-05-10
 */


public class SelectTaskTypPopWindow extends PopupWindow {
    private Activity context;
    private String type;// 1 时间  2 距离
    // private RelativeLayout rlAll;
    //private TextView tvAll;
    private RelativeLayout rlUp;
    private TextView tvUp;
    private RelativeLayout rlDown;
    private TextView tvDown;
    private int height;
    // private ImageView ivAll;
    private ImageView ivUp;
    private ImageView ivBottom;
    private SelectLisenter selectLisenter;
    private int mOne_position;
    private int mTwo_position;


    public interface SelectLisenter {
        void onSelect(int one_position, int two_position,int selectType);
    }

    /**
     * @param one_position 第一个位置
     * @param two_position 第二个位置
     * @param height
     * @param context
     * @param type
     */
    public SelectTaskTypPopWindow(final SelectLisenter selectLisenter, final int one_position, final int two_position, int height, final Activity context, final String type) {
        this.context = context;
        this.type = type;
        this.height = height;
        this.selectLisenter = selectLisenter;
        this.mOne_position = one_position;
        this.mTwo_position = two_position;
        //获得 LayoutInflater 的实例
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.pop_selecttasktype, null);
        //   rlAll = (RelativeLayout) contentView.findViewById(R.id.rl_all);
        // tvAll = (TextView) contentView.findViewById(R.id.tv_all);
        rlUp = (RelativeLayout) contentView.findViewById(R.id.rl_up);
        tvUp = (TextView) contentView.findViewById(R.id.tv_up);
        rlDown = (RelativeLayout) contentView.findViewById(R.id.rl_down);
        tvDown = (TextView) contentView.findViewById(R.id.tv_down);
        //ivAll = (ImageView) contentView.findViewById(R.id.iv_all);
        ivUp = (ImageView) contentView.findViewById(R.id.iv_up);
        ivBottom = (ImageView) contentView.findViewById(R.id.iv_bottom);
//
//
//        rlAll.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if ("1".equals(type)) {
//                    mOne_position = 0;
//                    selectLisenter.onSelect(mOne_position, mTwo_position);
//
//                } else if ("2".equals(type)) {
//                    mTwo_position = 0;
//                    selectLisenter.onSelect(mOne_position, mTwo_position);
//                }
//                tvAll.setTextColor(Color.parseColor("#555555"));
//                tvUp.setTextColor(Color.parseColor("#B5B5B5"));
//                tvDown.setTextColor(Color.parseColor("#B5B5B5"));
//                ivAll.setVisibility(View.VISIBLE);
//                ivUp.setVisibility(View.GONE);
//                ivBottom.setVisibility(View.GONE);
//                dismiss();
//            }
//        });
        rlUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(type)) {
                    mOne_position = 1;
                    mTwo_position = 0;
                    selectLisenter.onSelect(mOne_position, mTwo_position,0);

                } else if ("2".equals(type)) {
                    mOne_position = 0;
                    mTwo_position = 1;
                    selectLisenter.onSelect(mOne_position, mTwo_position,2);
                }
                //  tvAll.setTextColor(Color.parseColor("#B5B5B5"));
                tvUp.setTextColor(Color.parseColor("#555555"));
                tvDown.setTextColor(Color.parseColor("#B5B5B5"));
                // ivAll.setVisibility(View.GONE);
                ivUp.setVisibility(View.VISIBLE);
                ivBottom.setVisibility(View.GONE);
                dismiss();
            }
        });
        rlDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(type)) {
                    mOne_position = 2;
                    mTwo_position = 0;
                    selectLisenter.onSelect(mOne_position, mTwo_position,1);

                } else if ("2".equals(type)) {
                    mOne_position = 0;
                    mTwo_position = 2;
                    selectLisenter.onSelect(mOne_position, mTwo_position,3);
                }
                tvUp.setTextColor(Color.parseColor("#B5B5B5"));
                tvDown.setTextColor(Color.parseColor("#555555"));
                ivUp.setVisibility(View.GONE);
                ivBottom.setVisibility(View.VISIBLE);
                dismiss();
            }
        });


        if ("1".equals(type)) {
            tvUp.setText("时间升序");
            tvDown.setText("时间降序");
            if (mOne_position == 0) {
                //  ivAll.setVisibility(View.VISIBLE);
                ivUp.setVisibility(View.GONE);
                ivBottom.setVisibility(View.GONE);
                // tvAll.setTextColor(Color.parseColor("#555555"));
                tvUp.setTextColor(Color.parseColor("#B5B5B5"));
                tvDown.setTextColor(Color.parseColor("#B5B5B5"));
            } else if (mOne_position == 1) {
                // ivAll.setVisibility(View.GONE);
                ivUp.setVisibility(View.VISIBLE);
                ivBottom.setVisibility(View.GONE);
                // tvAll.setTextColor(Color.parseColor("#B5B5B5"));
                tvUp.setTextColor(Color.parseColor("#555555"));
                tvDown.setTextColor(Color.parseColor("#B5B5B5"));
            } else if (mOne_position == 2) {
                // ivAll.setVisibility(View.GONE);
                ivUp.setVisibility(View.GONE);
                ivBottom.setVisibility(View.VISIBLE);
                //  tvAll.setTextColor(Color.parseColor("#B5B5B5"));
                tvUp.setTextColor(Color.parseColor("#B5B5B5"));
                tvDown.setTextColor(Color.parseColor("#555555"));
            }

        } else if ("2".equals(type)) {
            tvUp.setText("距离远");
            tvDown.setText("距离近");
            if (mTwo_position == 0) {
                //ivAll.setVisibility(View.VISIBLE);
                ivUp.setVisibility(View.GONE);
                ivBottom.setVisibility(View.GONE);
                //tvAll.setTextColor(Color.parseColor("#555555"));
                tvUp.setTextColor(Color.parseColor("#B5B5B5"));
                tvDown.setTextColor(Color.parseColor("#B5B5B5"));
            } else if (mTwo_position == 1) {
                //  ivAll.setVisibility(View.GONE);
                ivUp.setVisibility(View.VISIBLE);
                ivBottom.setVisibility(View.GONE);
                // tvAll.setTextColor(Color.parseColor("#B5B5B5"));
                tvUp.setTextColor(Color.parseColor("#555555"));
                tvDown.setTextColor(Color.parseColor("#B5B5B5"));
            } else if (mTwo_position == 2) {
                //  ivAll.setVisibility(View.GONE);
                ivUp.setVisibility(View.GONE);
                ivBottom.setVisibility(View.VISIBLE);
                // tvAll.setTextColor(Color.parseColor("#B5B5B5"));
                tvUp.setTextColor(Color.parseColor("#B5B5B5"));
                tvDown.setTextColor(Color.parseColor("#555555"));
            }
        }
        //获取屏幕的宽高
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        this.setContentView(contentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w - DisplayUtil.dip2px(context, 10));
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(height);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
//        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        ColorDrawable dw = new ColorDrawable(20000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
    }

}
